package partParsers;

import java.io.IOException;
import java.util.ArrayList;

import util.HtmlTextFinder;

//this class extracts titles of main advertisment table from real estate html
public class TitleStringFinder implements PartParser {
	protected ArrayList<Integer> titles=new ArrayList<Integer>();	
	protected String tablePrefix=null;//html text that preceeds data table
	protected HtmlTextFinder txtFinder;
	
	public TitleStringFinder(String buf,String tablePrefix) {		
		txtFinder=new HtmlTextFinder(buf);
		this.tablePrefix=new String(tablePrefix);
		// TODO Auto-generated constructor stub
	}

	//returns type of i'th column in main advertisment's table
	public int getMember(int i){return titles.get(i).intValue();}

	//returns number of columns in main advertisment's table
	public int getColNumber(){return titles.size();}

	//returns array with type of columns in main advertisment's table
	public Object[] toArray(){return titles.toArray();}
	
	//Method search for advertisments column names string
	//initializes 'titlesType' attribute
	public boolean parse() throws IOException
	{
		int i=-1,length=txtFinder.getBufLen();
		int cellPos=-1,endCellPos=-1,endPos=-1,temp=-1,pos;

		//look for prefix of advertisment table
		pos=txtFinder.indexOf(tablePrefix);
		if(pos<0)
			return false;		
		pos=txtFinder.indexOf("<TR",pos);
		cellPos=pos+4;
		if(pos<0)
			return false;
		txtFinder.moveTo(pos+4);		
		//search for  first known adv. data type after main table prefix
		do{				
			i=txtFinder.getType();
			pos=txtFinder.getPos();
		}while(pos<length && i<0);
		
		if(i<0)
			return false;
					
		//Search for start of the row with known datatype found			
		while(cellPos<pos)
		{
			temp=cellPos;
			cellPos=txtFinder.indexOf("<TR",cellPos+3);
			if(cellPos<0)
				cellPos=length;
		}
		
		//Search for the start of the row above
		endPos=temp;
		cellPos=temp;
		while(endPos<pos)
		//{			
			endPos=txtFinder.indexOf("</TR>",endPos+5);
			//if(endPos<0)
			//	endPos=length;
			//temp=endPos;
		//}	
		//endPos=temp;
		
		if(i==HtmlTextFinder.INFO)
			i=-1;
						
		//Check types of titles and add to ArrayList
		//fill empty columns values to titles types string
		do{		
			//Search for end of the column with known datatype found,
			//counting empty columns before it and adding (-1)s to titles string			
			cellPos=txtFinder.indexOf("<TD",cellPos);
			endCellPos=txtFinder.indexOf("</TD>",cellPos);
			while(endCellPos<txtFinder.getPos())
			{				
				titles.add(new Integer(-1));
				cellPos+=3;
				cellPos=txtFinder.indexOf("<TD",cellPos);
				endCellPos=txtFinder.indexOf("</TD>",cellPos);
				
			}			
			cellPos+=3;
			titles.add(new Integer(i));			
			i=txtFinder.getType();
			if(i==HtmlTextFinder.INFO)
				i=-1;			
			pos=txtFinder.getPos();
		}while(pos<endPos);
			
		if(i>=0 && i!=HtmlTextFinder.INFO)
			titles.add(new Integer(i));
		
		return true;				
	}

	public String getData(int i, int j) {
		// TODO Auto-generated method stub
		return titles.get(i).toString();
	}

}
